package com.limao.Admin.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.limao.Admin.DataClass.AdminSysParmClass;
import com.limao.DbConnection.DbConnectionManager;


public class AdminSysParmProvider {
	
	private static final String T_sys_params = "cd_sys_params";

	private static final String Load_sys_params = "SELECT * FROM " + T_sys_params;
	
	private static class AdminSysParmProviderContainer {
		private static AdminSysParmProvider instance = new AdminSysParmProvider();
	}

	public static AdminSysParmProvider getInstance() {
		return AdminSysParmProviderContainer.instance;
	}
	
	// 初始化
	private AdminSysParmProvider() {}
	

	public  List<AdminSysParmClass> Admin_LoadSysParm()
	{
		ArrayList<AdminSysParmClass> list = new ArrayList<AdminSysParmClass>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_params ");
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				AdminSysParmClass obj = new AdminSysParmClass();
				obj.setId(rs.getInt("id"));
				obj.setKey(rs.getString("key1"));
				obj.setValue1(rs.getString("value1"));
				obj.setAbout(rs.getString("about"));
				
				list.add(obj);
			}

		} catch (Exception e) {
			e.printStackTrace();
			list.clear();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return list;		
	}
	
	
	public  AdminSysParmClass Admin_LoadSysParmById(int id)
	{
		AdminSysParmClass obj = new AdminSysParmClass();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_params where id=?");
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				obj.setId(rs.getInt("id"));
				obj.setKey(rs.getString("key1"));
				obj.setValue1(rs.getString("value1"));
				obj.setAbout(rs.getString("about"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return obj;		
	}
	
	
	public  boolean Admin_UpdateSysParmById(int id,String key,String value,String about)
	{
		boolean flag=false;
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
		try {
			
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("update cd_sys_params set key1=?,value1=?,about=?  where id=?");
			pstmt.setString(1, key);
			pstmt.setString(2, value);
			pstmt.setString(3, about);
			pstmt.setInt(4, id);
			pstmt.executeUpdate();
			
			flag=true;

		} catch (Exception e) {
			flag=false;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return flag;		
	}	
	
	
	public  boolean Admin_INsertSysParmById(String key,String value,String about)
	{
		boolean flag=false;
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
		try {
			
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("INSERT INTO cd_sys_params (key1,value1,about) VALUES (?,?,?)");
			pstmt.setString(1, key);
			pstmt.setString(2, value);
			pstmt.setString(3, about);
			pstmt.execute();
			
			flag=true;

		} catch (Exception e) {
			flag=false;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return flag;		
	}	
	
	// 基础参数
	public HashMap<String, String> load_Params() {
		HashMap<String, String> result = new HashMap<String, String>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(Load_sys_params);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				result.put(rs.getString("key1"), rs.getString("value1"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return result;
	}
	
	// 基础参数
	public boolean update_cutRmb_Params(HashMap<String, String> params) {
		boolean flag=false;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("update cd_sys_params set value1=? where key1=? ");
		 	for (String key : params.keySet()) 
        	{
		 		pstmt.setString(1, params.get(key));
		 		pstmt.setString(2, key);
				pstmt.executeUpdate();

			}
		 	
		 	flag=true;
		 	

		} catch (Exception e) {
			flag=false;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return flag;
	}
		
	
	
}
